package cn.hgpu.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResourceInterceptor implements HandlerInterceptor {


    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 如果已登录，放行
        Object login_user = request.getSession().getAttribute("LOGIN_USER");

        if(login_user !=null){
            return true;
        }

        // 如果是访问与登录相关的功能，放行
        String requestURI = request.getRequestURI();
        if(requestURI.indexOf("/login") > 0 ){
            return true;
        }

        // 如果不是访问登录相关功能，打回登录界面
        request.setAttribute("msg","您还没有登录，请先登录！！");
        request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request,response);

        return false;
    }
}
